<template>
  <div>
    <div
      v-show="isShow"
      :id="id"
      style="width:100%;height: 300px;"
    ></div>
    <div v-if="!isShow" class="noData">暂无数据</div>
  </div>
</template>

<script>
import * as echarts from "echarts";
import { Color } from '@/utils/colorVariate'
export default {
  data() {
    return {
      myChart: null
    };
  },
  props: {
    id: {
      type: String,
      default: 'pie'
    },
    initData: {
      type: Object,
      default: () => {}
    }
  },
  mounted() {
    if(this.initData) {
      this.$nextTick(() => {
        this.initMap();
      })
    }
  },
  watch: {
    initData: {
      handler() {
        this.$nextTick(() => {
          this.initMap();
        })
      }
    }
  },
  computed: {
    isShow() {
      console.log(this.initData, 'shuju');
      return Object.keys(this.initData || {}).length !== 0
    }
  },
  methods: {
    initMap() {
      const _this = this
      const chartDom = document.getElementById(this.id);
      this.myChart = echarts.init(chartDom);
      this.myChart.showLoading();
      const echartsData = []
      for (const k in this.initData) {
        echartsData.push({ value: +this.initData[k], name: k})
      }
      let option = {
        tooltip: {
          trigger: "item",
        },
        color: Color,
        series: [
          {
            type: "pie",
            radius: ['30%', '90%'],
            data: echartsData,
            itemStyle: {
              borderRadius: 10,
              borderColor: '#fff',
              borderWidth: 2
            },
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
            label: {
              show: true,
              position: 'inner',
              formatter: '{b}：{d}%', // 格式化数值百分比输出
            }
          },
        ],
      };

      option && this.myChart.setOption(option);
      this.myChart.hideLoading();
      window.addEventListener("resize", function() {
        _this.myChart.resize();
      });
    },
  },
};
</script>

<style lang="less" scoped>
.noData {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 200px;
  font-weight: 700;
  font-size: 16px;
}
</style>  